package com.hanson.api;

import com.alibaba.fastjson.JSONObject;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.exception.BusinessException;
import com.hanson.entity.GoodsBean;
import com.hanson.entity.GroupGoodsBean;
import com.hanson.service.GoodsCommentService;
import com.hanson.service.GoodsService;
import com.hanson.service.GroupGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = "拼团管理")
@RestController
@RequestMapping("/api/groupGoods")
public class GroupGoodsApiController {
    @Autowired
    private GroupGoodsService groupGoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCommentService goodsCommentService;

    @ApiOperation(value = "分页查询拼团订单")
    @RequestMapping(value = "/select/{page}/{limit}", method = RequestMethod.GET)
    public ResultMessage select(@PathVariable Integer page, @PathVariable Integer limit) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("新增地址成功！");
        resultMessage.setData(groupGoodsService.getGroupGoodsPage(page, limit));
        return resultMessage;
    }

    @ApiOperation(value = "拼团商品详情")
    @RequestMapping(value = "/groupDetail/{id}", method = RequestMethod.GET)
    public ResultMessage groupDetail(@PathVariable("id") String id, HttpServletRequest request) throws WxErrorException {
        GroupGoodsBean groupGoodsBean = groupGoodsService.selectById(Integer.parseInt(id));
        if (groupGoodsBean != null) {
            groupGoodsService.fillAttrs(groupGoodsBean);
            int viewNum = null == groupGoodsBean.getViewNum() ? 0 : groupGoodsBean.getViewNum();
            groupGoodsBean.setViewNum(viewNum + 1);
            groupGoodsService.mofiySimpleData(groupGoodsBean);
        }
        GoodsBean goodsBean = goodsService.detail(groupGoodsBean.getGoodsId());
        if (null == goodsBean) {
            throw new BusinessException("商品已下架");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupData", groupGoodsBean);
        jsonObject.put("data", goodsBean);
        ModelMap modelMap = new ModelMap();
        jsonObject.put("comment", goodsCommentService.selectByGoodsId(goodsBean.getId(), 0, modelMap));
        jsonObject.put("countData", modelMap.get("countData"));
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(jsonObject);
        return resultMessage;
    }
}
